/*
 *  Copyright 2011 Spotor Inc. JasonYun
 */
package spotor.client.io;

import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
import static org.xmlpull.v1.XmlPullParser.END_TAG;
import static org.xmlpull.v1.XmlPullParser.START_TAG;
import static org.xmlpull.v1.XmlPullParser.TEXT;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import spotor.client.provider.SpotorContract;
import spotor.client.provider.SpotorContract.Plans;
import spotor.client.util.Lists;
import spotor.client.util.ParserUtils;


import android.content.ContentProviderOperation;
import android.content.ContentResolver;

public class LocalPlanHandler extends XmlHandler {

	public LocalPlanHandler(String authority) {
		super(authority);
		// TODO Auto-generated constructor stub
	}

	
	/** 
	  *解析Planxml文件
	  *
	 */
	public LocalPlanHandler() {
		super(SpotorContract.CONTENT_AUTHORITY);
		// TODO Auto-generated constructor stub
	}

	@Override
	public ArrayList<ContentProviderOperation> parse(XmlPullParser parser,
			ContentResolver resolver) throws XmlPullParserException,
			IOException {
		// TODO Auto-generated method stub

		final ArrayList<ContentProviderOperation> batch = Lists.newArrayList();
		int type;
		while ((type = parser.next()) != END_DOCUMENT) {
			if (type == START_TAG && Tags.TITLE.equals(parser.getName())) {
				parsePlan(parser, batch, resolver);
			}
		}
		return batch;
	}

	/**
	 * Parse a given {@link Plans} entry, building
	 * {@link ContentProviderOperation} to define it locally.
	 */
	private static void parsePlan(XmlPullParser parser,
			ArrayList<ContentProviderOperation> batch, ContentResolver resolver)
			throws XmlPullParserException, IOException {
		final int depth = parser.getDepth();
		ContentProviderOperation.Builder builder = ContentProviderOperation
				.newInsert(Plans.CONTENT_URI);
		String tag = null;
		int type;
		while (((type = parser.next()) != END_TAG || parser.getDepth() > depth)
				&& type != END_DOCUMENT) {
			if (type == START_TAG) {
				tag = parser.getName();
			} else if (type == END_TAG) {
				tag = null;
			} else if (type == TEXT) {
				final String text = parser.getText();
				if (Tags.ID.equals(tag)) {
					builder.withValue(Plans.PLAN_ID, text);
				} else if (Tags.TITLE.equals(tag)) {
					builder.withValue(Plans.PLAN_TITLE, text);
				}
			}
		}

		batch.add(builder.build());
	}

	/** XML tags expected from local source. */
    private interface Tags {
        String PLANS = "Result";
        String PLAN = "Plan";
        String ID = "id";
        String TITLE = "Title";
        String LOGO = "PlanLogo";
        String STATUS = "Status";
        String STARTLATITUDE = "StartLatitude";
        String STARTLONGITUDE = "StartLongitude";
        String ENDLATITUDE = "EndLatitude";
        String EndLONGITUDE = "EndLongitude";
    }

}
